package com.shengsheng.expschedule.service.expschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shengsheng.expschedule.base.result.ResultCodeEnum;
import com.shengsheng.expschedule.service.base.exception.ExpScheduleException;
import com.shengsheng.expschedule.service.expschedule.entity.Class;
import com.shengsheng.expschedule.service.expschedule.entity.Major;
import com.shengsheng.expschedule.service.expschedule.entity.Schedule;
import com.shengsheng.expschedule.service.expschedule.entity.Vo.ClassVo;
import com.shengsheng.expschedule.service.expschedule.mapper.ClassMapper;
import com.shengsheng.expschedule.service.expschedule.mapper.MajorMapper;
import com.shengsheng.expschedule.service.expschedule.mapper.ScheduleMapper;
import com.shengsheng.expschedule.service.expschedule.service.ClassService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 班级表 服务实现类
 * </p>
 *
 * @author yuan
 * @since 2021-09-29
 */
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class> implements ClassService {

    @Autowired
    private MajorMapper majorMapper;
    @Autowired
    private ScheduleMapper scheduleMapper;
    @Override
    public List<ClassVo> getClassList() {

        QueryWrapper<Class> classQueryWrapper = new QueryWrapper<>();
        classQueryWrapper.orderByAsc("class_name");
        List<Class> classList = this.list(classQueryWrapper);

        List<ClassVo> classVoList = new ArrayList<>();
        for (Class aClass : classList) {
            ClassVo classVo = new ClassVo();
            BeanUtils.copyProperties(aClass,classVo);
            String majorId = aClass.getMajorId();
            Major major = majorMapper.selectById(majorId);
            classVo.setMajorId(majorId);
            classVo.setMajorName(major.getMajorName());
            classVoList.add(classVo);
        }
        return classVoList;
    }

    @Override
    public boolean removeClassById(String id) {

        Integer className = baseMapper.selectById(id).getClassName();
        // 查询该班级是否存在于选的课程中
        QueryWrapper<Schedule> scheduleQueryWrapper = new QueryWrapper<>();
        scheduleQueryWrapper.select("class_name");
        List<Schedule> scheduleList = scheduleMapper.selectList(scheduleQueryWrapper);
        for (Schedule schedule : scheduleList) {
            if (schedule.getClassName().contains(className.toString())) {
                return false;
            }
        }
        return this.removeById(id);
    }

    @Override
    public Class saveClass(Class c) {

        // 查询班级名是否重复
        QueryWrapper<Class> classQueryWrapper = new QueryWrapper<>();
        classQueryWrapper.eq("class_name", c.getClassName());
        List<Class> classList = baseMapper.selectList(classQueryWrapper);
        // 说明已经有重名班级，无法添加
        if (classList.size() > 0) throw new ExpScheduleException(ResultCodeEnum.CLASS_NAME_EXIST);
        if (this.save(c)) return c;
        return null;
    }
}
